package com.sgb.kjwl.view.ui.fragment.manage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentShopCompanyManageBinding;
import com.sgb.kjwl.databinding.FragmentShopCompanyPropertyBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.manage.CompanyTradesListEntity;
import com.sgb.kjwl.viewmodel.manage.CompanyPropertyAllModel;
import com.sgb.kjwl.viewmodel.manage.CompanyPropertyModel;
import com.swgk.core.base.di.AppComponent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class CompanyPropertyFragment extends AppFragment {

    @Inject
    CompanyPropertyModel model;

    FragmentShopCompanyPropertyBinding binding;

    private String companyNo;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }

    public static CompanyPropertyFragment newInstance(String companyNo) {
        CompanyPropertyFragment fragment = new CompanyPropertyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("companyNo", companyNo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_shop_company_property, container, false);
        if (getArguments() != null)
            companyNo = getArguments().getString("companyNo");
        return binding.getRoot();
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        model.getCompanyAssetsType();
    }

    public void getCompanyAssetsType(List<IpPickSelectEntity> list) {

        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            fragments.add(CompanyPropertyAllFragment.newInstance(companyNo,list.get(i).getValue()));
        }
        binding.vpShopCompanyManage.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return list.get(position).getName();
            }
        });

        binding.tlShopCompanyManage.setupWithViewPager(binding.vpShopCompanyManage);
    }
}